﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;
using Oplaco.Web.DTO.IMPL;
using System.Data.SqlClient;

namespace Oplaco.Web.DTO
{    
    /// <summary>
    /// Handles retrieval of stats
    /// </summary>
    public static class StatisticsManager
    {
        internal const string StatsCacheKey = "StatsCache";
        internal const int StatsCacheDuration = 10; // in minutes
                
        /// <summary>
        /// Clears cache
        /// </summary>
        /// <param name="context"></param>
        public static void Clear(HttpContext context)
        {
            context.Cache.Remove(StatsCacheKey);
        }

        /// <summary>
        /// Get all stats into collection object
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <returns>Statistics Colletion object</returns>
        public static CachedStatisticsCollection Fetch(HttpContext context)
        {
            if (context == null)
                return null;
            try
            {
                CachedStatisticsCollection coll = context.Cache[StatsCacheKey] as CachedStatisticsCollection;
                if (coll == null)
                {
                    Stats stats = Stats.GetStatistics();
                    CachedStatistics cstats = new CachedStatistics();
                    cstats.ActiveAdResponses = stats.ActiveAdResponses;
                    cstats.ActiveAds = stats.ActiveAds;
                    cstats.DeletedAds = stats.DeletedAds;
                    cstats.LastGenerated = stats.LastGenerated;
                    cstats.PublishDate = DateTime.Now;
                    cstats.TotalAds = stats.TotalAds;
                    cstats.TopCategories = stats.TopCategories;
                    cstats.TotalResponses = stats.TotalResponses;
                    cstats.TotalUsers = cstats.TotalUsers;

                    coll = new CachedStatisticsCollection();
                    coll.Add(cstats);

                    DateTime expiration = DateTime.Now.AddMinutes(StatsCacheDuration);
                    context.Cache.Add(StatsCacheKey, coll, null, expiration, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                }
                else {
                    if (coll.Count == 1)
                    {

                        coll[0].PublishDate = DateTime.Now;
                    }
                    
                }

                return coll;
            
            }
            catch (SqlException SQLEX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(SQLEX);
            }
            catch (Exception EX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyBusinessLayerException(EX);
            }
            return null;
        }


        /// <summary>
        /// Gets stats on users
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static CachedStatisticsCollection FetchByUserId(HttpContext context, int userId)
        {
            if (context == null)
                return null;
            try
            {
                CachedStatisticsCollection s = context.Cache[StatsCacheKey] as CachedStatisticsCollection;
                return s;
            }
            catch (SqlException SQLEX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(SQLEX);
            }
            catch (Exception EX)
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyBusinessLayerException(EX);
            }
            return null;
        }
    }

}
